package com.ruoyi.web.exams.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.exams.domain.ExamQuestion;
import com.ruoyi.web.exams.dto.CreateQuestionWithOption;
import com.ruoyi.web.exams.service.IExamQuestionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 题Controller
 *
 * @author 范书铭
 * @date 2024-10-02
 */
@Api("题目")
@RestController
@RequestMapping("/exam/question")
public class ExamQuestionController extends BaseController {
    @Autowired
    private IExamQuestionService examQuestionService;

    /**
     * 查询题列表
     */
//    @ApiOperation("查询题列表")
    @PreAuthorize("@ss.hasPermi('exam:question:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExamQuestion examQuestion) {
        startPage();
        List<ExamQuestion> list = examQuestionService.selectExamQuestionList(examQuestion);
        return getDataTable(list);
    }

    /**
     * 导出题列表
     */
//    @ApiOperation("导出题列表")
    @PreAuthorize("@ss.hasPermi('exam:question:export')")
    @Log(title = "题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExamQuestion examQuestion) {
        List<ExamQuestion> list = examQuestionService.selectExamQuestionList(examQuestion);
        ExcelUtil<ExamQuestion> util = new ExcelUtil<ExamQuestion>(ExamQuestion.class);
        util.exportExcel(response, list, "题数据");
    }

    /**
     * 获取题详细信息
     */
//    @ApiOperation("获取题详细信息")
    @PreAuthorize("@ss.hasPermi('exam:question:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(examQuestionService.selectExamQuestionById(id));
    }

    /**
     * 新增题
     */
//    @ApiOperation("新增题")
    @PreAuthorize("@ss.hasPermi('exam:question:add')")
    @Log(title = "新增题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExamQuestion examQuestion) {
        return toAjax(examQuestionService.insertExamQuestion(examQuestion));
    }

    /**
     * 修改题
     */
    @ApiOperation("修改题")
    @PreAuthorize("@ss.hasPermi('exam:question:edit')")
    @Log(title = "修改题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExamQuestion examQuestion) {
        return toAjax(examQuestionService.updateExamQuestion(examQuestion));
    }

    /**
     * 删除题
     */
    @ApiOperation("删除题")
    @PreAuthorize("@ss.hasPermi('exam:question:remove')")
    @Log(title = "删除题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(examQuestionService.deleteExamQuestionByIds(ids));
    }

    /**
     * 新增题与选项
     */
    @ApiOperation("新增题与选项")
    @PreAuthorize("@ss.hasPermi('exam:question:add')")
    @Log(title = "新增题与选项", businessType = BusinessType.INSERT)
    @PostMapping("/createWithOptions")
    public AjaxResult addWithOption(@RequestBody CreateQuestionWithOption examQuestion) {
        return toAjax(examQuestionService.createQuestion(examQuestion));
    }
}
